using Microsoft.Phone.Controls.Primitives;
using System;
using System.Windows;
using System.Windows.Controls;
namespace Microsoft.Phone.Controls
{
	[TemplateVisualState(Name = "Left", GroupName = "Position States"), TemplateVisualState(Name = "Right", GroupName = "Position States"), TemplateVisualState(Name = "Center", GroupName = "Position States")]
	public class PivotItem : ContentControl
	{
		private const string PivotPositionsGroupName = "Position States";
		private const string PivotStateCenter = "Center";
		private const string PivotStateLeft = "Left";
		private const string PivotStateRight = "Right";
		private const string ContentName = "Content";
		private AnimationDirection? _firstAnimation = default(AnimationDirection?);
		private AnimationDirection _direction;
		public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(PivotItem), new PropertyMetadata(null));
		public object Header
		{
			get
			{
				return base.GetValue(PivotItem.HeaderProperty);
			}
			set
			{
				base.SetValue(PivotItem.HeaderProperty, value);
			}
		}
		public PivotItem()
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_STARTUP, PerfLog.PivotItem);
			base.set_DefaultStyleKey(typeof(PivotItem));
			base.add_Loaded(new RoutedEventHandler(this.PivotItem_Loaded));
		}
		private void PivotItem_Loaded(object sender, RoutedEventArgs e)
		{
			base.remove_Loaded(new RoutedEventHandler(this.PivotItem_Loaded));
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_STARTUP, PerfLog.PivotItem);
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_ARRANGE, PerfLog.PivotItem);
			Size result = base.ArrangeOverride(finalSize);
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_ARRANGE, PerfLog.PivotItem);
			return result;
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_MEASURE, PerfLog.PivotItem);
			Size result = base.MeasureOverride(availableSize);
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_MEASURE, PerfLog.PivotItem);
			return result;
		}
		public override void OnApplyTemplate()
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_APPLYTEMPLATE, PerfLog.PivotItem);
			base.OnApplyTemplate();
			AnimationDirection? firstAnimation = this._firstAnimation;
			this.MoveTo(AnimationDirection.Center);
			if (firstAnimation.get_HasValue())
			{
				this.MoveTo(firstAnimation.get_Value());
			}
			this._firstAnimation = new AnimationDirection?(AnimationDirection.Center);
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_APPLYTEMPLATE, PerfLog.PivotItem);
		}
		internal void MoveTo(AnimationDirection direction)
		{
			bool flag = direction != AnimationDirection.Center;
			this._direction = direction;
			if (!this._firstAnimation.get_HasValue() && flag)
			{
				this._firstAnimation = new AnimationDirection?(direction);
				return;
			}
			this.UpdateVisualStates(flag);
		}
		private void UpdateVisualStates(bool useTransitions)
		{
			VisualStateManager.GoToState(this, PivotItem.StateNameFromAnimationDirection(this._direction), useTransitions);
		}
		private static string StateNameFromAnimationDirection(AnimationDirection direction)
		{
			switch (direction)
			{
			case AnimationDirection.Center:
				return "Center";
			case AnimationDirection.Left:
				return "Left";
			case AnimationDirection.Right:
				return "Right";
			default:
				throw new InvalidOperationException();
			}
		}
	}
}
